<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@include file="/common/header.jsp"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>

<div id="menuTree">
    <div id="ztreeObj" class="ztree"></div>
</div>
<script>

    function saveTree(urls){
        var treeObj=$.fn.zTree.getZTreeObj("streeObj");
        if(treeObj == null){
            return;
        }
        nodes=treeObj.getCheckedNodes(true);
        var coll = [];
        for(var i=0;i<nodes.length;i++){
            if(nodes[i].pId == null){
                continue;
            }
            coll.push(nodes[i].id);
        }
        $.ajax({
            url:urls,
            data:{"ins_uuid":$("#ins_uuid").val(),"service_param_uuids":coll.toString()},
            success:function(result){
                if("success"==result){
                    $.jpmessage.info('保存成功!',function(){
                        window.location.reload();
                    });
                }else{
                    $.jpmessage.info('保存失败!');
                }
            }

        });
    }

    $(document).ready(function() {

        //初始化时候 根据ins_uuid 来显隐 机构选择按钮
        if($("#ins_uuid").val() != null && $("#ins_uuid").val() != ""){
            $("#selelctBtn").remove();
            freshServiceTree();
        }

        var ztreeObj;
        /*选择按钮*/
        $("#selelctBtn").click(function(){
//            ztreeObj = showInsTree(selected, $("#ins_uuid").val());
            var option = {url:'../insinf/getDirectInsInfTree.do'};
            ztreeObj = showInsTree(selected, null, option);
        });

        /*确定按钮回调*/
        function selected(){
            var nodes = ztreeObj.getSelectedNodes();
            console.log(nodes);
            if(nodes && nodes.length > 0){
                $("#inst_name").textbox("setValue", nodes[0].NAME);
                $("#ins_uuid").val(nodes[0].ID);
            }
            freshServiceTree();
        }



//        //保存树
//        $("#saveTreeBtn").click(function(){
//            var treeObj=$.fn.zTree.getZTreeObj("streeObj");
//            if(treeObj == null){
//                return;
//            }
//            nodes=treeObj.getCheckedNodes(true);
//            var coll = [];
//            for(var i=0;i<nodes.length;i++){
//                if(nodes[i].pId == null){
//                    continue;
//                }
//                coll.push(nodes[i].id);
//            }
//            $.ajax({
//                url:"../insServiceParam/updateInsParamSetTree.do",
//                data:{"ins_uuid":$("#ins_uuid").val(),"service_param_uuids":coll.toString()},
//                success:function(result){
//                    if("success"==result){
//                        $.jpmessage.info('保存成功!');
//                    }else{
//                        $.jpmessage.info('保存失败!');
//                    }
//                }
//
//            });
//        });

        //直接返回
        $("#backBtn").click(function(){
            $("#backBtn").attr("href","../insServiceParam/getInsServiceParamList.do?menuCode="+$("#menuCode").val());
        });


        //刷新业务树
        function freshServiceTree(){
            var streeObj;
            var setting = {
                data:{
                    key: {name: "name"},
                    simpleData:{enable: true, idKey: "id", pIdKey: "pId"}
                },
                check : {
                    enable : true,
                    chkStyle : "checkbox",    //复选框
                    chkboxType : {
                        "Y" : "s",
                        "N" : "ps"
                    }
                },
                callback :{
                    onClick : treenodeClick
                },
                treeNode:{
                    open : true
                }

            }

            function treenodeClick(event, treeId, treeNode, clickFlag) {
                if(treeNode.pId != null){
                    $('#myframe').attr('src', "../insServiceParam/getInsServiceParamByUUID.do?ins_uuid="+$("#ins_uuid").val()+
                        "&service_uuid="+treeNode.pId+"&service_param_uuid="+treeNode.id+"&menuCode="+menuCodeValue);
                }
            }



            /*请求菜单树形*/
            $.ajax({
                url: "../insServiceParam/getServiceInfTreeEdit.do?ins_uuid="+$("#ins_uuid").val(),
                success: function(result){
                    streeObj = $.fn.zTree.init($("#streeObj"), setting, result);
                }
            });
        }

    });
</script>

<body class="easyui-layout">
    <div data-options="region:'north',border:false" style="height:60px;padding:10px">
        <input id="ins_uuid" type="hidden" name="ins_uuid" value="${ins_uuid}"/>
        所属机构：<input class="easyui-textbox" type="text" readonly="readonly" id="inst_name" value="${inst_name}"/>
        <a href="javascript:void(0)" class="easyui-linkbutton" iconCls="icon-search" id="selelctBtn">选择</a>
    </div>
    <div data-options="region:'west',split:true,title:'业务',collapsible:false" style="width:300px;padding:10px;">
        <div id="streeObj" class="ztree"></div>
    </div>
    <div data-options="region:'south',border:false" style="height:50px;padding:10px;">
        <div style="text-align:left;padding:5px">
            <c:if test="${insLevel == '1' ||  (insLevel != '1' && insUuid != ins_uuid)}">
                <jiepos:buttonDetailTag  menuCode="${menuCode}" buttonCode="INSSERVPARM_BTN_SAVE_TREE" hideBack="true" formId="insServiceParamForm"></jiepos:buttonDetailTag>
            </c:if>
            <%--<a href="javascript:void(0)"  class="easyui-linkbutton btn detail-remove" iconCls="icon-save" id="saveTreeBtn">保存</a>--%>
            <input type="hidden" id="menuCode" name="menuCode" value="${menuCode}"/>
            <a href="javascript:void(0)" class="easyui-linkbutton btn" iconCls="icon-back" id="backBtn">返回</a>
        </div>
    </div>
    <div data-options="region:'center',title:'设置'" class="easyui-layout" style="overflow: auto;">
        <iframe id="myframe" width="100%" height="98%" frameBorder="0" src="../insServiceParam/getInsServiceParamByUUID.do" scrolling="yes"></iframe>
    </div>
</body>
